
# coding=utf-8
from application import cache


def get_cache_normal(cache_name, timeout) :
    """
    特殊处理的Json  缓存
    :param cache_name:
    :param timeout:
    :return:
    """
    def wrapper(function):
        def inner_wrapper(*args, **kwargs):
            data = cache.get(cache_name % args)
            if not data:
                data = function(*args, **kwargs)
                if data :
                    cache.set(cache_name % args, data, timeout)
                return data
            else:
                return data
        return inner_wrapper
    return wrapper


def get_cache(method, properties, cache_name, timeout):
    """
    自动处理Json信息  缓存
    :param method:
    :param properties:
    :param cache_name:
    :param timeout:
    :return:
    """
    def wrapper(function):
        def inner_wrapper(*args, **kwargs):
            data_list = cache.get(cache_name % args)
            if not data_list:
                model_list = function(*args, **kwargs)
                if model_list :
                    data_list = method(model_list, properties)
                    cache.set(cache_name % args, data_list, timeout)
                    return data_list
                return None
            else:
                return data_list
        return inner_wrapper
    return wrapper
